<?php

// $Id: vpage_news_list_form_ctrl.class.php 1570 2009-01-13 12:44:02Z ogrebg $

require_once(TANGRA_MAIN_DIR.'form/gform_ctrl_with_obj.class.php');

require_once('vpage_news_list_form.class.php');
require_once('vpage_news_list_form_view.class.php');

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpt/vpage_news_list_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/category/tcms_category_dbc.class.php');


class VPage_News_List_Form_Ctrl extends GForm_Ctrl_With_Object {
	private $categories_ol_map;

	protected function create_form() {
		$form = new VPage_News_List_Form();

		$dbc = $this->get_dbc();

		$ol_map = TCMS_Category_DBC::load_categories_ol_map($dbc);
		$category_field = $form->get_field('category');
		array_unshift($ol_map['ids'], 0);
		$ol_map['ol_map'][0] = '---';

		$category_field->set_options($ol_map['ids']);
		$this->categories_ol_map = $ol_map['ol_map'];


//		$form->generate_html();
		return $form;
	}


	protected function create_form_view() {
		$ret = new VPage_News_List_Form_View($this->form);

		$category_field = $ret->get_field_view('category');
		$category_field->set_ol_map($this->categories_ol_map);

		return $ret;
	}


	protected function on_good_submit() {
		$ret = true;


		return $ret;
	}


	protected function create_object() {
		$object = new VPage_News_List_DBC();

		return $object;
	}


	protected function transfer_obj2form() {
		$object = $this->get_object();
		$form = $this->get_form();

		$dbc = $this->get_dbc();
		$category = new TCMS_Category_DBC();
		if ($category->load_by_hid($dbc, $object->get_category())) {
			$form->set_field_value('category', $category->get_id());
		} else {
			$form->set_field_value('category', 0);
		}

		$form->set_field_value('items', $object->get_items());
	}


	protected function transfer_form2obj() {
		$object = $this->get_object();
		$form = $this->get_form();

		$dbc = $this->get_dbc();
		$category = new TCMS_Category_DBC();
		if ($category->load_by_id($dbc, $form->get_field_value('category'))) {
			$object->set_category($category->get_hid());
		} else {
			throw new Tangra_Module_Exception('Cannot load category with ID = '.$form->get_field_value('category'));
		}
		$object->set_items($form->get_field_value('items'));
	}


	protected function _save_object(DB_Connection $dbc, I_DB_Storable $object) {
		$object->save($dbc, $this->get_permanent_external_param('user_id'));
	}
}